<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:q="livcos.org/ns/query"
	xmlns:db="livcos.org/ns/db"
	xmlns:ddl="livcos.org/ns/db/ddl"
	xmlns:f="livcos.org/ns/xslt/function"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	exclude-result-prefixes="q db ddl f xs">

  <xsl:import href="../../../../common/base.xsl" />
  
	<xsl:template match="q:*" mode="data">
		<xsl:variable name="age" select="current-dateTime() - xs:dateTime(q:cache/@time)"/>
		<xsl:variable name="v" select="number(q:cache/@valid)"/>
		<!-- $v = NaN or $v = number('') does not seem to work... -->
		<xsl:variable name="valid"
			select="q:cache/@time and ((not($v) and ($v != 0)) or ($v &lt; 0) or (f:total-seconds($age) &lt; $v))"/>
		<data ref="{f:object-id(.)}#{@id}" time="{if ($valid) then xs:dateTime(q:cache/@time) else current-dateTime()}"
			xmlns="" refresh="/livcos.org/type/livcos.org/ns/query/refresh">
			<xsl:choose>
				<xsl:when test="$valid">
					<xsl:sequence select="f:parse(q:cache)"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="." mode="query"/>
				</xsl:otherwise>
			</xsl:choose>
		</data>
	</xsl:template>
	
	<xsl:template match="q:xpath[not(@ref)]" mode="data">
		<!-- ignore cache for in object XPaths -->
		<data ref="{f:object-id(.)}#{@id}" xmlns="">
			<xsl:apply-templates select="." mode="query"/>
		</data>
	</xsl:template>
	
	
	<xsl:template match="q:xpath[@xpath]" mode="query">
		<nodes xmlns="">
			<xsl:for-each select="f:eval(.., @xpath, if (@ns) then f:parse-ns(@ns) else .)">
				<node obj="{f:object-id(.)}" entity="{f:entity-id(.)}" node="{f:node-id(.)}">
					<xsl:apply-templates select="." mode="query-result"/>
				</node>
			</xsl:for-each>
		</nodes>
	</xsl:template>
	
	<xsl:template match="node()" mode="query-result">
		<xsl:copy-of select="."/>
	</xsl:template>
	
	<xsl:template match="@*" mode="query-result">
		<attr xmlns=""><xsl:value-of select="."/></attr>
	</xsl:template>
	
	<xsl:template match="q:xpath" mode="query">
		<nodes xmlns="">
			<node>
				<xpath>-- no XPath defined yet --</xpath>
			</node>
		</nodes>
	</xsl:template>
	
	
	<xsl:template match="q:search[@query]" mode="query">
		<xsl:sequence select="f:query(if (@index) then @index else '/livcos.org', @query)"/>
	</xsl:template>
	
	<xsl:template match="q:search" mode="query">
		<hits xmlns="">
			<hit>
				<text-search>-- no query defined yet --</text-search>
			</hit>
		</hits>
	</xsl:template>
	
	
	<xsl:template match="q:sql[q:query]" mode="query">
		<xsl:sequence select="f:db-query(q:query, @connection, true(), true(), true())"/>
	</xsl:template>
	
	<xsl:template match="q:sql" mode="query">
		<db:results>
			<ddl:meta>
				<ddl:column label="-- SQL Query --"/>
			</ddl:meta>
			<db:row>
				<db:field>-- no query defined yet --</db:field>
			</db:row>
		</db:results>
	</xsl:template>
	
	
</xsl:stylesheet>
